home *** CD-ROM | disk | FTP | other *** search
/ CD Exchange / CD Exchange - Volume 1.iso / graphics / utils / ham8-jpeg / source / al-backdrop.c next >
C/C++ Source or Header  |  1992-12-20  |  8KB  |  339 lines

  1. /*
  2.  * Al-backdrop.c Michael Saunby    M.Saunby@reading.ac.uk
  3.  *
  4.  * Release 1.1  December 1992 Jpeg backdrop using Albert HAM8 public screen and
  5.  * Independent JPEG Group's jpeg decompressor.
  6.  */
  7. #define PROG_NAME "Al-backdrop JPEG display"
  8.  
  9. #include <intuition/intuition.h>
  10. #include <intuition/gadgetclass.h>
  11. #include <graphics/gfxbase.h>
  12. #include <graphics/displayinfo.h>
  13. #include <intuition/screens.h>
  14. #include <dos/dosextens.h>
  15. #include <exec/memory.h>
  16. #include <dos/dosasl.h>
  17. #include <dos/rdargs.h>
  18. #include <libraries/gadtools.h>
  19.  
  20. #include <clib/asl_protos.h>
  21. #include <clib/dos_protos.h>
  22. #include <clib/exec_protos.h>
  23. #include <clib/intuition_protos.h>
  24. #include <clib/layers_protos.h>
  25. #include <clib/graphics_protos.h>
  26. #include <clib/gadtools_protos.h>
  27. #include <clib/alib_protos.h>
  28.  
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32.  
  33. #include "display24.h"
  34.  
  35. /* This should work with bigger numbers, but... */
  36. #define ARRAY24_MAX_ROWS 1
  37.  
  38. /*
  39.  * ReadArgs() template
  40.  */
  41. #define TEMPLATE "NAME"
  42. #define OPT_NAME 0
  43. #define OPT_COUNT 1
  44.  
  45. /*
  46.  * Message structure used to get screen size changed, see ham8.c
  47.  */
  48. struct TestMsg
  49. {
  50.   struct Message msg;
  51.   USHORT Width;
  52.   USHORT Height;
  53.   BOOL Lores;
  54. };
  55.  
  56. struct IntuitionBase *IntuitionBase = NULL;
  57. struct GfxBase *GfxBase = NULL;
  58. struct Library *AslBase = NULL;
  59. extern struct ExecBase *SysBase;
  60.  
  61. /* following for 24 bit put row */
  62. int JPEG_restart = 0;
  63. SHORT ham8_line = 0;        /* screen line number to draw at */
  64. UBYTE *ham8_pens = NULL;
  65. struct RastPort ham8_temprp;
  66. struct BitMap *temp_bm = NULL;
  67. struct Screen *screen = NULL;
  68. struct FileRequester *filereq = NULL;
  69. struct BitMap *bitmap = NULL;
  70.  
  71. struct EasyStruct failedES =
  72. {sizeof (struct EasyStruct), 0, PROG_NAME,
  73.  "%s", "OK",};
  74.  
  75. void
  76. Cleanup ()
  77. {
  78.   if (screen)
  79.     {
  80.       ReleasePens (&(screen->ViewPort));
  81.       UnlockPubScreen (NULL, screen);
  82.     }
  83.   if (ham8_pens)
  84.     FreeMem (ham8_pens, ((temp_bm->BytesPerRow << 3) * temp_bm->Rows));
  85.   if (temp_bm)
  86.     FreeBitMap (temp_bm);
  87.   if (filereq)
  88.     FreeAslRequest (filereq);
  89.   if (AslBase)
  90.     CloseLibrary (AslBase);
  91.   if (IntuitionBase)
  92.     CloseLibrary ((struct Library *) IntuitionBase);
  93.   if (GfxBase)
  94.     CloseLibrary ((struct Library *) GfxBase);
  95. }
  96.  
  97. void
  98. Quit (char whytext[], UBYTE failcode)
  99. {
  100.   EasyRequest (NULL, &failedES, NULL, whytext);
  101.   Cleanup ();
  102.   exit (failcode);
  103. }
  104.  
  105.  
  106. char *
  107. Startup (char *namebuf)
  108. {
  109.   LONG result[OPT_COUNT] =
  110.   {0};
  111.   struct RDArgs *rda;
  112.  
  113.   if ((GfxBase =
  114.        (struct GfxBase *) OpenLibrary ("graphics.library", 36)) == NULL)
  115.     Quit ("graphics.library is too old <V36", 25);
  116.  
  117.   if ((IntuitionBase =
  118.     (struct IntuitionBase *) OpenLibrary ("intuition.library", 36)) == NULL)
  119.     Quit ("intuition.library is too old <V36", 25);
  120.  
  121.   if ((AslBase = OpenLibrary ("asl.library", 36)) == NULL)
  122.     Quit ("asl.library is too old <V36", 25);
  123.  
  124.   rda = ReadArgs (TEMPLATE, result, NULL);
  125.  
  126.   if (result[OPT_NAME])
  127.     {
  128.       strcpy (namebuf, (UBYTE *) result[OPT_NAME]);
  129.       FreeArgs (rda);
  130.     }
  131.   else
  132.     {
  133.       struct Screen *screen;
  134.       FreeArgs (rda);        /* In case we quit */
  135.       /*
  136.        * Opening the File Requester does not bring screen to front. Passing
  137.        * the name ie. UnlockPubscreen(HAM8_ALBERT_NAME, NULL) is allowed
  138.        * but not recommended. I haven't tried it.
  139.        */
  140.       if ((screen = LockPubScreen (HAM8_ALBERT_NAME)) != NULL)
  141.     {
  142.       ScreenToFront (screen);
  143.       UnlockPubScreen (NULL, screen);
  144.     }
  145.       if ((filereq =
  146.        (struct FileRequester *) AllocAslRequestTags (ASL_FileRequest,
  147.                       ASLFR_PubScreenName, HAM8_ALBERT_NAME,
  148.                              TAG_END))
  149.       == NULL)
  150.     Quit ("could not build file requster", 25);
  151.       if (AslRequest (filereq, 0L))
  152.     {
  153.       if(filereq->rf_Dir[strlen(filereq->rf_Dir)-1] == ':')
  154.       sprintf (namebuf, "%s%s", filereq->rf_Dir, filereq->rf_File);
  155.       else
  156.       sprintf (namebuf, "%s/%s", filereq->rf_Dir, filereq->rf_File);
  157.     }
  158.     }
  159.  
  160.   return (namebuf);
  161. }
  162.  
  163.  
  164. void
  165. CreateWindow (UWORD width, UWORD height)
  166. {
  167.   int pen_errors;
  168.   const int guess_barheight = 24;    /* First guess at menu bar
  169.                      * height */
  170.   int screen_height;
  171.   BOOL lores = FALSE;        /* why bother? */
  172.   BOOL screenok = FALSE;    /* set to true if screen accepts new
  173.                  * dimensions */
  174.   struct MsgPort *testport, *progport;
  175.   struct TestMsg *msg;
  176.  
  177.   /*
  178.    * Size of screen needed to take this picture.
  179.    */
  180.   screen_height = height + guess_barheight + 1;
  181.  
  182.   if (testport = CreatePort (NULL, NULL))
  183.     {
  184.       if (msg = AllocMem (sizeof (struct TestMsg), MEMF_PUBLIC | MEMF_CLEAR))
  185.     {
  186.       msg->msg.mn_ReplyPort = testport;
  187.       msg->msg.mn_Length = sizeof (struct TestMsg);
  188.       msg->Width = width;
  189.       msg->Height = screen_height;
  190.       msg->Lores = lores;
  191.       Forbid ();
  192.       if (progport = FindPort (HAM8_ALBERT_NAME))
  193.         {
  194.           PutMsg (progport, (struct Message *) msg);
  195.           Permit ();
  196.           /* This could be quite a wait - the screen may have visitors. */
  197.           WaitPort (testport);
  198.           msg = (struct TestMsg *) GetMsg (testport);
  199.           screenok = TRUE;
  200.         }
  201.       /* extra Permit() in case FindPort() failed */
  202.       Permit ();
  203.       FreeMem (msg, (ULONG) sizeof (struct TestMsg));
  204.     }
  205.       DeletePort (testport);
  206.     }
  207.   if (!screenok)
  208.     Quit ("could not find screen", 25);
  209.  
  210.   Delay (100);
  211.   if ((screen = LockPubScreen (HAM8_ALBERT_NAME)) == NULL)
  212.     Quit ("could not find screen", 25);
  213.  
  214.  
  215.   /*
  216.    * Dont draw on menu bar.
  217.    */
  218.   ham8_line = (screen->BarHeight < guess_barheight) ? screen->BarHeight :
  219.     guess_barheight;
  220.   ham8_line++;
  221.  
  222.   CopyMem (&(screen->RastPort), &ham8_temprp, sizeof (struct RastPort));
  223.   ham8_temprp.Layer = NULL;
  224.   if ((temp_bm = AllocBitMap (width, ARRAY24_MAX_ROWS, 8, 0L, bitmap)) == NULL)
  225.     Quit ("cannot get line bitmap", 25);
  226.   ham8_temprp.BitMap = temp_bm;
  227.   ham8_pens = AllocMem (((temp_bm->BytesPerRow << 3) * temp_bm->Rows), 0L);
  228.   /*
  229.    * Get a copy of the special palette. The palette is set when the screen
  230.    * is created.
  231.    */
  232.   pen_errors = StandardPalette (&(screen->ViewPort), TRUE);
  233.   if (pen_errors)
  234.     EasyRequest (NULL, &failedES,
  235.          NULL,
  236.          "Could not find full HAM-8 Albert palette");
  237.  
  238. }
  239.  
  240.  
  241. #undef GLOBAL
  242. #include "jinclude.h"
  243.  
  244.  
  245. #ifndef EIGHT_BIT_SAMPLES
  246. Sorry, this code only copes with 8 - bit JSAMPLEs.    /* deliberate syntax err */
  247. #endif
  248.  
  249.  
  250. /*
  251.  * Write the file header.
  252.  */
  253.  
  254. METHODDEF void
  255. output_init (decompress_info_ptr cinfo)
  256. {
  257.   CreateWindow ((SHORT) cinfo->image_width, (SHORT) cinfo->image_height);
  258.  
  259. }
  260.  
  261.  
  262. /*
  263.  * Write some pixel data.
  264.  */
  265.  
  266. METHODDEF void
  267. put_pixel_rows (decompress_info_ptr cinfo, int num_rows,
  268.         JSAMPIMAGE pixel_data)
  269. {
  270.   JSAMPROW ptr0, ptr1, ptr2;
  271.   long width = cinfo->image_width;
  272.   int row, height;
  273.  
  274.  
  275.   if (cinfo->out_color_space == CS_GRAYSCALE)
  276.     {
  277.       for (row = 0; row < num_rows; row += height)
  278.     {
  279.       ptr0 = pixel_data[0][row];
  280.       height = num_rows - row;    /* ie rows left */
  281.       height = (ARRAY24_MAX_ROWS < height) ? ARRAY24_MAX_ROWS : height;
  282.  
  283.       WritePixelArray24 (&(screen->RastPort), 0, ham8_line,
  284.                  width, height,
  285.                  ptr0, ptr0, ptr0, ham8_pens, &ham8_temprp);
  286.       ham8_line += height;
  287.     }
  288.     }
  289.   else
  290.     {
  291.       for (row = 0; row < num_rows; row += height)
  292.     {
  293.       ptr0 = pixel_data[0][row];
  294.       ptr1 = pixel_data[1][row];
  295.       ptr2 = pixel_data[2][row];
  296.       height = num_rows - row;    /* ie rows left */
  297.       height = (ARRAY24_MAX_ROWS < height) ? ARRAY24_MAX_ROWS : height;
  298.  
  299.       WritePixelArray24 (&(screen->RastPort), 0, ham8_line,
  300.                  width, height,
  301.                  ptr0, ptr1, ptr2, ham8_pens, &ham8_temprp);
  302.       ham8_line += height;
  303.     }
  304.  
  305.     }
  306. }
  307.  
  308.  
  309. /*
  310.  * Finish up at the end of the file.
  311.  */
  312.  
  313. METHODDEF void
  314. output_term (decompress_info_ptr cinfo)
  315. {
  316.   /* Cleanup(); */
  317. }
  318.  
  319.  
  320. /*
  321.  * The method selection routine for HAM8 format output. This should be called
  322.  * from d_ui_method_selection if HAM8 output is wanted.
  323.  */
  324.  
  325. GLOBAL void
  326. jselwham8 (decompress_info_ptr cinfo)
  327. {
  328.  
  329.  
  330.   cinfo->methods->output_init = output_init;
  331.   cinfo->methods->put_pixel_rows = put_pixel_rows;
  332.   cinfo->methods->output_term = output_term;
  333.   if ((cinfo->out_color_space != CS_GRAYSCALE) &&
  334.       (cinfo->out_color_space != CS_RGB))
  335.     ERREXIT (cinfo->emethods, "HAM8 output must be grayscale or RGB");
  336.  
  337.  
  338. }
  339.